package com.jbl.ma.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbl.commonutils.R;
import com.jbl.ma.entity.TalkRecord;
import com.jbl.ma.mapper.TalkRecordMapper;
import com.jbl.ma.service.TalkRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jbl.ma.vo.TalkRecordVo;
import com.jbl.ma.vo.TalkVo;
import com.jbl.te.entity.Teacher;
import com.jbl.te.service.TeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jbl
 * @since 2020-12-11
 */
@Service
public class TalkRecordServiceImpl extends ServiceImpl<TalkRecordMapper, TalkRecord> implements TalkRecordService {
    @Autowired
    private TeacherService teacherService;

    @Transactional
    @Override
    public List<TalkRecordVo> getRecord(Boolean isTeacher, String teacherNumber) {
        // 1.根据teacherNumber查出相关列表（管理员端查所有的teacherNumber，教师端根据自己的teacherNumber查）
        List<TalkRecord> teacherList = null;

        if(!isTeacher) {
            teacherList = this.list(
                    new QueryWrapper<TalkRecord>()
                            .select("distinct teacher_number")
                            .groupBy("teacher_number")
            );
        } else {
            teacherList = this.list(
                    new QueryWrapper<TalkRecord>()
                            .eq("teacher_number",teacherNumber)
            );
        }

        List<TalkRecordVo> collect = teacherList.stream().map(teacher -> {
            TalkRecordVo recordVo = new TalkRecordVo();
            // 2.根据教师id查出教师信息
            Teacher teacherInfo = teacherService.getById(teacher.getTeacherNumber());
            recordVo.setTeacherNumber(teacher.getTeacherNumber());
            recordVo.setTeacherName(teacherInfo.getName());
            // 3.获取最新消息
            TalkRecord one = this.getOne(
                    new QueryWrapper<TalkRecord>()
                            .orderByDesc("gmt_create")
                            .eq("teacher_number", teacher.getTeacherNumber())
                            .last("LIMIT 1")
            );
            recordVo.setLastMsgTime(one.getGmtCreate()); // 最新时间
            recordVo.setLastMsgContent(one.getContent()); // 最新消息

            //如果最后一条信息是本人发的，则置空
            if((!isTeacher && one.getRole() == 0) || (isTeacher && one.getRole() == 1)) {
                recordVo.setNewMsgCount(null);
            } else {
                // 获取未读
                int isRead = this.count(
                        new QueryWrapper<TalkRecord>()
                                .eq("teacher_number", teacher.getTeacherNumber())
                                .eq("is_read", 0) //未读
                                .eq("role", isTeacher?0:1) //用户
                );
                recordVo.setNewMsgCount(isRead);
            }

            return recordVo;
        }).collect(Collectors.toList());

        return collect;
    }

    @Transactional
    @Override
    public List<TalkVo> getChat(String teacherNumber) {
        List<TalkRecord> msgList = this.list(
                new QueryWrapper<TalkRecord>()
                        .eq("teacher_number", teacherNumber)
                        .orderByAsc("gmt_create")
        );

        List<TalkVo> collect = msgList.stream().map(msg -> {
            TalkVo talkVo = new TalkVo();
            BeanUtils.copyProperties(msg, talkVo);
            // 修改信息状态为已读,role==1[admin],role==0[user]
            if (msg.getRole() == 1) {
                msg.setIsRead(1);// 已读-1-管理员，已读-2-用户
                this.updateById(msg);
            }
            if (msg.getRole()==0) {
                msg.setIsRead(2);// 已读-1-管理员，已读-2-用户
                this.updateById(msg);
            }
            return talkVo;
        }).collect(Collectors.toList());
        return collect;
    }

}
